/*
 * Copyright (c) 2008, Solido Systems
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of Solido Systems nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.solidosystems.objectkitchen.data;

import java.util.*;
import java.io.*;

public class VList extends Value implements List<Value>{
	private List<Value> internalList;
	private boolean dirty;
	
	public VList(){
		dirty=false;
		internalList=new ArrayList<Value>();
	}
	
	public boolean isDirty(){
		if(dirty)return true;
		for(Value val:internalList){
			if(val.isDirty())return true;
		}
		return false;
	}
	
	public VList(List<Value> list){
		internalList=list;
	}
	
	public boolean equalsSameType(Value val){
		return equals(val);
	}
	
	public void writeToStream(DataOutputStream stream) throws IOException{
		stream.writeByte(Value.LIST);
		stream.writeInt(internalList.size());
		for(Value val:internalList){
			// stream.writeByte(val.getTypeId());
			val.writeToStream(stream);
		}
	}
	
	public static Value readDataFromStream(DataInputStream stream) throws IOException{
		long size=stream.readInt();
		VList list=new VList();
		for(int i=0;i<size;i++){
			list.add(Value.readFromStream(stream));
		}
		return list;
	}
	
	public boolean add(Value value){
		dirty=true;
		return internalList.add(value);
	}
	
	public boolean add(String value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(int value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(long value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(float value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(double value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(boolean value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public boolean add(byte[] value){
		dirty=true;
		return internalList.add(Value.create(value));
	}
	
	public void add(int index, Value value){
		dirty=true;
		internalList.add(index,value);
	}
	
	public void add(int index,String value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,boolean value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,int value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,long value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,float value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,double value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public void add(int index,byte[] value){
		dirty=true;
		internalList.add(index,Value.create(value));
	}
	
	public boolean addAll(Collection<? extends Value> c){
		dirty=true;
		return internalList.addAll(c);
	}
	
	public boolean addAll(int index, Collection<? extends Value> c){
		dirty=true;
		return internalList.addAll(c);
	}
	
	public void clear(){
		if(internalList.size()>0)dirty=true;
		internalList.clear();
	}
	
	public boolean contains(Object obj){
		return internalList.contains(obj);
	}
	
	public boolean containsAll(Collection<?> c){
		return internalList.containsAll(c);
	}
	
	public boolean equals(Object obj){
		if(obj.getClass().equals(this.getClass())){
			return internalList.equals(((VList)obj).internalList);
		}
		return false;
	}
	
	public Value get(int index){
		return internalList.get(index);
	}
	
	public int hashCode(){
		return internalList.hashCode();
	}
	
	public int indexOf(Object obj){
		return internalList.indexOf(obj);
	}
	
	public boolean isEmpty(){
		return internalList.isEmpty();
	}
	
	public Iterator<Value> iterator(){
		return internalList.iterator();
	}
	
	public int lastIndexOf(Object obj){
		return internalList.lastIndexOf(obj);
	}
	
	public ListIterator<Value> listIterator(){
		return internalList.listIterator();
	}
	
	public ListIterator<Value> listIterator(int index){
		return internalList.listIterator(index);
	}

	public Value remove(int index){
		dirty=true;
		return internalList.remove(index);
	}
	
	public boolean remove(Object obj){
		dirty=true;
		return internalList.remove(obj);
	}
	
	public boolean removeAll(Collection<?> c){
		dirty=true;
		return internalList.removeAll(c);
	}
	
	public boolean retainAll(Collection<?> c){
		dirty=true;
		return internalList.retainAll(c);
	}
	
	public Value set(int index, Value value){
		return internalList.set(index,value);
	}
	
	public int size(){
		return internalList.size();
	}
	
	public List<Value> subList(int indexFrom, int indexTo){
		return new VList(internalList.subList(indexFrom,indexTo));
	}
	
	public Object[] toArray(){
		return internalList.toArray();
	}
	
	public <Value>Value[] toArray(Value[] a){
		return internalList.toArray(a);
	} 
	
	// Value abstracts
	
	public String getTypeName(){
		return "List";
	}
	
	public int getTypeId(){
		return LIST;
	}
	
	public String getString(int key){
		Value val=get(key);
		if(val!=null)return val.toString();
		return null;
	}
	
	public Long getLong(int key){
		Value val=get(key);
		if(val!=null)return val.toLong();
		return null;
	}
	
	public Integer getInteger(int key){
		Value val=get(key);
		if(val!=null)return val.toInteger();
		return null;
	}
	
	public Float getFloat(int key){
		Value val=get(key);
		if(val!=null)return val.toFloat();
		return null;
	}
	
	public Double getDouble(int key){
		Value val=get(key);
		if(val!=null)return val.toDouble();
		return null;
	}
	
	public Boolean getBoolean(int key){
		Value val=get(key);
		if(val!=null)return val.toBoolean();
		return null;
	}
	
	public byte[] getByteArray(int key){
		Value val=get(key);
		if(val!=null)return val.toByteArray();
		return null;
	}
	
	public String toString(){
		StringBuilder buf=new StringBuilder();
		buf.append("[");
		for(Value v:this){
			if(buf.length()>1)buf.append(",");
			buf.append(v.toString());
		}
		buf.append("]");
		return buf.toString();
	}
	
	public Integer toInteger(){
		return null;
	}
	
	public Long toLong(){
		return null;
	}
	
	public Float toFloat(){
		return null;
	}
	
	public Double toDouble(){
		return null;
	}
	
	public Boolean toBoolean(){
		return null;
	}
	
	public byte[] toByteArray(){
		return null;
	}
}